package com.xzcedu.billsystem;


import java.io.*;

/**
 * @author 徐州城
 * @version 1.0
 * 专门用来保存 账单的数据
 */
public class KeepData extends MainProcedure {
    private static String filePath = "src\\com\\xzcedu\\billsystem\\data.txt";//用于存放数据的地址
    private static File file = new File(filePath);//用来判断filePath的路径的文件 是否存在
    private static BufferedReader br = null;//处理流
    private static BufferedWriter bw = null;//处理流

    //将MainProcedure的属性 赋给data
    private static String data = MainProcedure.getInformation();

    //专门用来写数据的方法
    public static void writeDate() {
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write((MainProcedure.getMoney() + "") + "\r\n");
            bw.write(MainProcedure.getInformation());//不用再添加换行符 因为在前面已经添加过来


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {//关闭流
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //专门用于读取数据的方法
    public static void readData() {

        try {
            br = new BufferedReader(new FileReader(filePath));
            String line = "";
            String str = "";
            String s = br.readLine();//先读一行 把总余额取出
            MainProcedure.setMoney(Double.parseDouble(s));//把取出的余额 再赋值给MainProcedure
            while ((line = br.readLine()) != null) {//一行一行的读
                str += line + "\r\n";
            }
            //把str的值 设置给 MainProcedure.Information
            MainProcedure.setInformation(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {//关闭流
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String getData() {
        return data;
    }

    public static String getFilePath() {
        return filePath;
    }

    public static File getFile() {
        return file;
    }
}
